package com.feishi.project.websocket.init;


import com.feishi.core.media.opencv.util.OpenCvUtil;
import com.feishi.core.util.common.EncryptUtil;
import com.feishi.project.websocket.websocket.OpenCv;


import org.opencv.core.Mat;
import org.opencv.videoio.VideoCapture;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0
 * @auth caixq
 * @time 18-5-3 09:49
 **/
@Component
public class Init {
    int i;
//    @PostConstruct
    public void init() throws IOException {
        VideoCapture camera = new VideoCapture();
        camera.open(0);

        Map map =new HashMap();
        if (!camera.isOpened()) {
            System.out.println("Camera Error");
        } else {
            Mat frame = new Mat();

            while (true){
                long start=System.currentTimeMillis();
                camera.read(frame);
                byte[] bytes = OpenCvUtil.MatToBytes(frame,".png");
                System.out.print(System.currentTimeMillis()-start);
                start=System.currentTimeMillis();
                System.out.print(":");

                String head="data:image/png;base64,";
                map.put("img",EncryptUtil.encodeBase64(bytes));
                OpenCv.broadcast(map);
                if(i==2){
                    System.out.println(EncryptUtil.encodeBase64(bytes));
                    i++;
                }

                System.out.println(System.currentTimeMillis()-start);
                try {
                    Thread.sleep(40);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    @Scheduled(cron="0/5 * *  * * ? ")
    public void scheduled() throws IOException {
        Map map =new HashMap();
        map.put("img","svgushsdfhsi");

        i++;
        System.out.println(i);
        if(i==2){
            init();
        }
//        OpenCv.broadcast(map);
    }
}
